﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Test_2D
{
    class Common
    {
    }

    /// <summary>
    /// 补间类
    /// </summary>
    [Serializable]
    public class Tween
    {
        Frame startFrame, endFrame;
        TimeLine timeLine;      //补间所在的时间轴

        Motion startMotion, endMotion;


        int length;     //补间的长度(补间的起始帧不算在内)

        public Tween(Frame _startFrame, Frame _endFrame, TimeLine _timeLine)
        {
            startFrame = _startFrame;
            endFrame = _endFrame;
            timeLine = _timeLine;
        }

        public Tween(Motion _startMotion,  Motion _endMotion, int _length, TimeLine _timeLine)
        {
            startMotion = _startMotion;
            endMotion = _endMotion;
            timeLine = _timeLine;
            length = _length;
        }

        public int Length
        {
            get { return getTweenLength(); }
            //set { length = value; }
        }

        /// <summary>
        /// 计算补间的长度(补间的起始帧不算在内)
        /// </summary>
        /// <returns></returns>
        private int getTweenLength()
        {
            int start = timeLine.Frames.IndexOf(startFrame);
            int end = timeLine.Frames.IndexOf(endFrame);
            return end - start;
        }

        /// <summary>
        /// 获得补间动画中距离起始帧长度为index的运动元
        /// </summary>
        public Motion getTweenMotion(int index)
        {
            length = getTweenLength();

            startMotion = startFrame.Motions[0];
            endMotion = endFrame.Motions[0];

            Point startP = new Point(startMotion.Location.X, startMotion.Location.Y);
            Point endP = endMotion.Location;

            Size startS = new Size(startMotion.Size.Width, startMotion.Size.Height);
            Size endS = endMotion.Size;

            int inc_x = (endP.X - startP.X) / length * index;    //运动元在x方向上的偏移量//运动元在每一帧x方向上的增量
            int inc_y = (endP.Y - startP.Y) / length * index;    //运动元在y方向上的偏移量//运动元在每一帧y方向上的增量

            int inc_width = (endS.Width - startS.Width) / length * index;       //运动元宽度的增量
            int inc_height = (endS.Height - startS.Height) / length * index;    //运动元长度的增量

            int actcount = startMotion.Component.ActCount;
            int startAct = startMotion.Act;

            int act = (startAct + index) % actcount + 1;

            startP.Offset(inc_x, inc_y);
            startS.Width += inc_width;
            startS.Height += inc_height;

            return new Motion(startMotion.Component, act, startP, startS);

            /*

            int cur_act = startMotion.Act;    //起始动作号
            

            for (int i = cur_frame + 1; i < nextkey; ++i)
            {
                cur_act++;

                if (cur_act > start_mot.Component.ActCount)
                    cur_act = 1;

                startP.Offset(inc_x, inc_y);

                //if (timeLine.Frames[i] == null)
                timeLine.Frames[i] = new Frame();

                timeLine.Frames[i].Motions.Add(
                    new Motion(start_mot.Component, cur_act, new Point(startP.X, startP.Y), start_mot.Size));
            }*/
        }
    }
}
